<?php
header('content-type:text/html;charset=utf-8');
$month = $_POST['month'] ?? date('Y-n');
$monStDate = $month.'-1';//本月一号
$monStTime = strtotime($monStDate);//本月一号对应时间戳
$weekMonSt = date('N',$monStTime);//本月一号星期几
$monDays = date('t',$monStTime);
$result = [];
$n = 1;
for($i=1;$i<=$monDays;$i++){
    $k = $i/7;
    if($k>$n){
        $n++;
    }
    $today = date($month.'-'.$i);
    $nextDay = date('Y-m-d',strtotime($today)+86400);
    $yesToday = date('Y-m-d',strtotime($today)-86400);
    $result[$n][$weekMonSt] =[
        'path'=>$n."-".$weekMonSt,
        'w'=>$i,
        'today'=>$today,
        'ym'=>$month,
        'next_date'=>$nextDay,
        'yestoday'=>$yesToday
    ];
    $weekMonSt++;
    if($weekMonSt>7){
        $n++;
        $weekMonSt = 1;
    }

}
for ($j=1;$j<=7;$j++){
    $line1 = $result[1];
    $line5 = $result[5];
    if(!isset($line1[$j])){
        $result[1][$j] = '';
    }
    if(!isset($line5[$j])){
        $result[5][$j] = '';
    }
}
foreach ($result as $line=>$items ){
    foreach ($items as $w=>$day){
        if($line-1>0){
            if(!isset($result[$line-1])){
                continue;
            }
            $demo = $result[$line-1];
            if(!isset($demo[$w])){
                continue;
            }
            $to = $demo[$w];
            if(is_array($to)&&is_array($day)){
                $day['up_day'] = $to['today'];
            }

        }else{
            if(is_array($day)){
                $day['up_day'] = '';
            }

        }
        if($line+1<=$n){
            if(!isset($result[$line+1])){
                continue;
            }
            $demo = $result[$line+1];
            if(!isset($demo[$w])){
                continue;
            }
            $to = $demo[$w];
            if(is_array($to)&&is_array($day)){
                $day['down_day'] = $to['today'];
            }
        }else{
            if(is_array($day)){
                $day['down_day'] = '';
            }
        }
        $items[$w] = $day;
    }
    $result[$line] = $items;

}
$file = 'table.php';
extract($result);
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean();

echo "<script>window.parent.showTable('".$contents."');</script>";


?>
